﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using bzxxxt.admin.service;
using bzxxxt.dal;
using bzxxxt.model;

namespace bzxxxt.bll
{
    public class RelativeBLL : BaseBLL
    {
        admin.entity.UserEntity user = admin.service.UserService.GetUser(HttpContext.Current);   //获取cookie里的用户对象

        public string GetRelativeByTel(string tel) {
            
            var rlist = from r in DB.tbRelative where r.tel == tel 
                       select new RelativeModel {
                            id = r.id,
                            name = r.name,
                            gender = r.gender,
                            cardno = r.cardno,
                            company = r.company,
                            tel = r.tel,                    
                            addr = r.addr,
                            areacode = r.areacode,
                            areaName = r.tbArea == null ? "" : r.tbArea.name
                        };
            return JsonUtil.ToJson(new { success = true, msg = rlist.FirstOrDefault() });            
        }

        public string GetRelativeById(int relativeId)
        {           
            var rlist = from r in DB.tbRelative
                        where r.id == relativeId
                        select new RelativeModel
                        {
                            id = r.id,
                            name = r.name,
                            gender = r.gender,
                            cardno = r.cardno,
                            company = r.company,
                            tel = r.tel,
                            addr = r.addr,
                            areacode = r.areacode,
                            areaName = r.tbArea == null ? "" : r.tbArea.name
                        };
            return JsonUtil.ToJson(new { success = true, msg = rlist.FirstOrDefault() });
        }
        public string GetAll()
        {
            var list = from r in DB.tbRelative
                       select new RelativeModel {
                           id = r.id,
                           name = r.name,
                           gender = r.gender,
                           cardtype = r.cardtype,
                           cardno = r.cardno,
                           company = r.company,
                           tel = r.tel,
                           addr = r.addr,
                           areacode = r.areacode,
                           postcode = r.postcode,
                           areaName = r.tbArea == null ? "" : r.tbArea.name
                       };
            string json = JsonUtil.ToJson(list);            
            return json;
        }

        public string GetListByPage(int pageSize, int pageIndex, string sort, string where = "1=1")
        {
            int totalCount = 0;
            SqlParameter paramTableName = new SqlParameter("@tableName", "tbRelative");
            SqlParameter paramColumns = new SqlParameter("@columns", "*");
            SqlParameter paramOrder = new SqlParameter("@order", sort);
            SqlParameter paramPageSize = new SqlParameter("@pageSize", pageSize);
            SqlParameter paramPageIndex = new SqlParameter("@pageIndex", pageIndex);
            SqlParameter paramWhere = new SqlParameter("@where", where);
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var p = DB.tbRelative.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                new SqlParameter[] { paramTableName, paramColumns, paramOrder, paramPageSize, paramPageIndex, paramWhere, paramTotalCount }).ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);

            var list = from r in p
                       select new RelativeModel
                       {
                           id = r.id,
                           name = r.name,
                           gender = r.gender,
                           cardtype = r.cardtype,
                           cardno = r.cardno,
                           company = r.company,
                           tel = r.tel,
                           addr = r.addr,
                           areacode = r.areacode,
                           postcode = r.postcode,
                           areaName = r.tbArea == null ? "" : r.tbArea.name
                       };
            var l = list.ToList();
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string Add(tbRelative r)
        {
            var rl = DB.tbRelative.FirstOrDefault(m => m.name == r.name && m.areacode == r.areacode && m.tel == r.tel);
            if (rl != null) {
                return JsonUtil.ToJson(new { success = false, msg = "联系人已经存在！" });
            }
            DB.tbRelative.Add(r);
            DB.SaveChanges();
            return JsonUtil.ToJson(new { success = true, msg="添加成功！"});
        }
        public string Edit(tbRelative r)
        {
            var d = DB.tbRelative.Where(m => m.id == r.id).FirstOrDefault();
            if (d != null)
            {
                d.name = r.name;
                d.cardtype = r.cardtype;
                d.cardno = r.cardno;
                d.addr = r.addr;
                d.areacode = r.areacode;
                d.postcode = r.postcode;
                d.company = r.company;
                d.gender = r.gender;
                d.tel = r.tel;
                d.updatetime = r.updatetime;
            }
            DB.SaveChanges();
            return JsonUtil.ToJson(new { success = true, msg = "修改成功！" });
        }
        public string Delete(string relativeIds)
        {
            string[] ids = relativeIds.Split(',');
            List<int> lstIds = new List<int>();
            foreach (var s in ids)
            {
                lstIds.Add(int.Parse(s));
            }
            var list = DB.tbRelative.Where(m => lstIds.Contains(m.id)).ToList();
            list.ForEach(m => {
                m.tbDead.Clear();
                m.tbGrave.Clear();
                DB.tbRelative.Remove(m);
            });
            DB.SaveChanges();
            return JsonUtil.ToJson(new { success = true, msg = "删除成功！" });
        }
    }
}